require("config")

function logopen()
	local path = common.getUserPath("stoicheia.log")
	local mode = "w"
	logfile = io.open(path, mode)
	if logfile == nil then
		log("log", "Log `%s' failed to open", path)
	else
		log("log", "Log `%s' successfully opened", path)
	end
end

function logclose()
	if logfile == nil then return end
	log("log", "Log close")
	logfile:flush()
	logfile:close()
	logfile = nil
end

function log(_module, format, ...)
	local time = os.date("%Y/%m/%d %H:%M:%S")
	local message = time .. " " .. _module .. ": " .. string.format(format, ...)
	print("Log: " .. message)
	if logfile then
		logfile:write(message, "\n")
		logfile:flush()
	end
end

function logerr(_module, format, ...)
	log(_module, "ERROR: " .. format, ...)
end


